﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIM.model;
using System.Reflection;

namespace LIM.controller
{
    public class Thanh_CRMHopDongChiTietController : BaseController
    {
        public Thanh_CRMHopDongChiTietController()
        { 
        }

        public tbl_CRM_HopDong GetHopDong(int pRowID)
        {
            if (pRowID == -1)
                return LIMS_Entities.tbl_CRM_HopDong.FirstOrDefault();
            return LIMS_Entities.tbl_CRM_HopDong.Where(p => p.RowID == pRowID).FirstOrDefault();
        }

        public IQueryable<Thanh_ChiTieuComboBox> GetsChiTieuComboBox()
        {
            return LIMS_Entities.tbl_DanhMuc_ChiTieu.Select(p => new Thanh_ChiTieuComboBox { RowID = p.RowID, MaChiTieu = p.MaChiTieu, TenChiTieu = p.TenGoi, LoaiMau = p.tbl_DanhMuc_LoaiMau.TenGoi });
        }

        public IQueryable<Thanh_TinhTrangComboBox> GetsTinhTrangComboBox()
        {
            DanhMucHeThongController oDanhMucHeThongController = new DanhMucHeThongController();
            return oDanhMucHeThongController.LoadDataChild("TinhTrangHopDong").AsQueryable().Select(p => new Thanh_TinhTrangComboBox()
                {
                    RowID = p.RowID,
                    MaDanhMuc = p.MaDanhMuc,
                    TenGoi = p.TenGoi
                });
        }

        public Thanh_ChiTieuKiemNghiem GetsChiTieuGridView(int pRowIDChiTieu)
        {
            DateTime CurrenDate = base.GetCurrentDateTimeFromDatabaseServer().Date.AddDays(1);
            return LIMS_Entities.tbl_DanhMuc_ChiTieu.Where(p => p.RowID == pRowIDChiTieu).Select(p => new Thanh_ChiTieuKiemNghiem
            {
                //RowID = p.RowID,
                //MaChiTieu = p.RowID,
                //TenChiTieu = p.RowID,
                //STT = index + 1,
                //RowIDLoaiMau = p.tbl_DanhMuc_LoaiMau.RowID,
                LoaiMau = p.tbl_DanhMuc_LoaiMau.TenGoi,
                PhuongPhap = p.PhuongPhap,
                SoLuong = 1,
                DonGia = p.tbl_DanhMuc_ChiTieu_BangGia.Where(g => g.NgayHieuLuc.Value < CurrenDate).FirstOrDefault().Gia.Value,
                //ThanhTien = p.tbl_DanhMuc_ChiTieu_BangGia.Where(g => g.NgayHieuLuc.Value.Date <= CurrenDate).First().Gia.Value
            }).FirstOrDefault();
        }

        public IQueryable<Thanh_KhachHang> GetsKhachHang()
        {
            return LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.LaKhachHangKiemNghiem.HasValue && p.LaKhachHangKiemNghiem.Value && p.TinhTrangKhachHang == "TinhTrangKhachHang_DangHoatDongBinhThuong")
                .Select(p => new Thanh_KhachHang { RowID = p.RowID, MaKhachHang = p.MaKhachHang, TenGoi = p.TenGoi, TenGoiNgan = p.TenGoiNgan, DiaChi = p.DiaChi, SoDienThoai = p.SoDienThoai, SoFax = p.SoFax });
        }

        public IQueryable<Thanh_LienHe> GetsLienHe(int pRowIDKhachHang)
        {
            var query = LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.RowID == pRowIDKhachHang).FirstOrDefault();
            if (query != null && query.tbl_CRM_LienHeKhachHang.Count > 0)
            {
                return query.tbl_CRM_LienHeKhachHang.AsQueryable()
                .Select(p => new Thanh_LienHe { RowID = p.RowID, ChucVu = p.ChucVu, EmailCaNhan = p.tbl_CRM_LienHe.EmailCaNhan, EmailCongTy = p.Email, HoTen = p.tbl_CRM_LienHe.HoTen, SoDiDong = p.tbl_CRM_LienHe.SoDiDong });
            }
            return null;
        }

        public bool SaveHopDong(Thanh_HopDong pThanh_HopDong, List<Thanh_ChiTieuKiemNghiem> pChiTieuKiemNghiems, List<Thanh_DieuKhoanThanhToan> pDieuKhoanThanhToans)
        {
            bool result = false;
            DateTime currentDate = base.GetCurrentDateTimeFromDatabaseServer();
            tbl_CRM_HopDong otbl_CRM_HopDong = LIMS_Entities.tbl_CRM_HopDong.Where(p => p.RowID == pThanh_HopDong.RowID).FirstOrDefault();
            if (otbl_CRM_HopDong == null)
            {
                otbl_CRM_HopDong = new tbl_CRM_HopDong();
                pThanh_HopDong.NgayTao = currentDate;
                pThanh_HopDong.NguoiTao = InitUserInfos.UserName;

                LIMS_Entities.tbl_CRM_HopDong.AddObject(otbl_CRM_HopDong);
                ////\\ Dung.
                //LIMS_Entities.Refresh(System.Data.Objects.RefreshMode.StoreWins, otbl_CRM_HopDong);
            }
            else
            {
                pThanh_HopDong.NgayTao = otbl_CRM_HopDong.NgayTao.Value;
                pThanh_HopDong.NguoiTao = otbl_CRM_HopDong.NguoiTao;
            }
            // Set values.
            pThanh_HopDong.NgayCapNhat = currentDate;
            pThanh_HopDong.NguoiCapNhat = InitUserInfos.UserName;
            Util.CopyValues(pThanh_HopDong, otbl_CRM_HopDong, true, "RowID");
            // ChiTieuKiemNghiem.
            List<tbl_CRM_HopDong_ChiTieuKiemNghiem> lstChiTieuKiemNghiem = otbl_CRM_HopDong.tbl_CRM_HopDong_ChiTieuKiemNghiem.ToList();
            otbl_CRM_HopDong.tbl_CRM_HopDong_ChiTieuKiemNghiem.Clear();
            foreach (var item in pChiTieuKiemNghiems)
            {
                tbl_CRM_HopDong_ChiTieuKiemNghiem otblChiTieuKiemNghiem = lstChiTieuKiemNghiem.Where(p => p.RowID == item.RowID).FirstOrDefault();
                if (otblChiTieuKiemNghiem == null)
                {
                    otblChiTieuKiemNghiem = new tbl_CRM_HopDong_ChiTieuKiemNghiem();
                    item.NgayTao = currentDate;
                    item.NguoiTao = InitUserInfos.UserName;
                }
                else
                {
                    item.NgayTao = otblChiTieuKiemNghiem.NgayTao.Value;
                    item.NguoiTao = otblChiTieuKiemNghiem.NguoiTao;
                }
                item.NgayCapNhat = currentDate;
                item.NguoiCapNhat = InitUserInfos.UserName;

                Util.CopyValues(item, otblChiTieuKiemNghiem, true, "RowID");
                otbl_CRM_HopDong.tbl_CRM_HopDong_ChiTieuKiemNghiem.Add(otblChiTieuKiemNghiem);
            }
            // DieuKhoanThanhToan.
            List<tbl_CRM_HopDong_DieuKhoanThanhToan> lstDieuKhoanThanhToan = otbl_CRM_HopDong.tbl_CRM_HopDong_DieuKhoanThanhToan.ToList();
            otbl_CRM_HopDong.tbl_CRM_HopDong_DieuKhoanThanhToan.Clear();
            foreach (var item in pDieuKhoanThanhToans)
            {
                tbl_CRM_HopDong_DieuKhoanThanhToan otblDieuKhoanThanhToan = lstDieuKhoanThanhToan.Where(p => p.RowID == item.RowID).FirstOrDefault();
                if (otblDieuKhoanThanhToan == null)
                {
                    otblDieuKhoanThanhToan = new tbl_CRM_HopDong_DieuKhoanThanhToan();
                    item.NgayTao = currentDate;
                    item.NguoiTao = InitUserInfos.UserName;
                }
                else
                {
                    item.NgayTao = otblDieuKhoanThanhToan.NgayTao.Value;
                    item.NguoiTao = otblDieuKhoanThanhToan.NguoiTao;
                }
                item.NgayCapNhat = currentDate;
                item.NguoiCapNhat = InitUserInfos.UserName;

                Util.CopyValues(item, otblDieuKhoanThanhToan, true, "RowID");
                otbl_CRM_HopDong.tbl_CRM_HopDong_DieuKhoanThanhToan.Add(otblDieuKhoanThanhToan);
            }
            //try
            {
                LIMS_Entities.SaveChanges();
                result = true;
            }
            //catch (Exception ex)
            {
            }
            return result;
        }

        public bool DeleteHopDong(int pRowIDHopDong)
        {
            tbl_CRM_HopDong otbl_CRM_HopDong = LIMS_Entities.tbl_CRM_HopDong.Where(p => p.RowID == pRowIDHopDong).FirstOrDefault();
            if (otbl_CRM_HopDong != null)
            {
                // ChiTieuKiemNghiem 
                for (int i = 0; i < otbl_CRM_HopDong.tbl_CRM_HopDong_ChiTieuKiemNghiem.Count; i++)
                {
                    tbl_CRM_HopDong_ChiTieuKiemNghiem ctkm = otbl_CRM_HopDong.tbl_CRM_HopDong_ChiTieuKiemNghiem.ElementAt(i--);
                    otbl_CRM_HopDong.tbl_CRM_HopDong_ChiTieuKiemNghiem.Remove(ctkm);
                    LIMS_Entities.DeleteObject(ctkm);
                }
                // DieuKhoanThanhToan 
                for (int i = 0; i < otbl_CRM_HopDong.tbl_CRM_HopDong_DieuKhoanThanhToan.Count; i++)
                {
                    tbl_CRM_HopDong_DieuKhoanThanhToan ctkm = otbl_CRM_HopDong.tbl_CRM_HopDong_DieuKhoanThanhToan.ElementAt(i--);
                    otbl_CRM_HopDong.tbl_CRM_HopDong_DieuKhoanThanhToan.Remove(ctkm);
                    LIMS_Entities.DeleteObject(ctkm);
                }
                LIMS_Entities.tbl_CRM_HopDong.DeleteObject(otbl_CRM_HopDong);
                LIMS_Entities.SaveChanges();
                return true;
            }
            return false;
        }
    }
}
